import 'package:common/base/base_present.dart';
import 'package:common/constant/constant.dart';
import 'package:common/utils/log_util.dart';
import 'package:dio/dio.dart';

import '../../constant/http_constant.dart';
import '../../network/code_msg_model.dart';
import '../../network/dio_manager.dart';
import 'common_api_path.dart';

//文件下载
class DownLoadPresent extends BasePresent {
  DownLoadPresent(super.businessResponse);

  //通过路径下载文件
  Future<void> downloadFileByPath(
      String path, String savePath, ProgressCallback? onReceiveProgress) async {
    try {
      Map<String,String> queryParameters = {'fileId':path};
      DioManager.external(HttpConstant.baseUrl).downloadByPath(CommonApiPath.downloadPath, savePath, businessResponse,
          queryParameters: queryParameters,onReceiveProgress: onReceiveProgress);
    } on Exception catch (exception) {
      LogUtil.stdPrint(exception);
      CodeMsgModel codeMsgModel = CodeMsgModel();
      codeMsgModel.code = Constant.fail.toString();
      codeMsgModel.msg = exception.toString();
      businessResponse.onFail(codeMsgModel);
    }
  }

  //通过uri下载文件
  Future<void> downloadFileByUri(
      Uri uri, String savePath, ProgressCallback? onReceiveProgress) async {
    try {
      DioManager().downloadByUri(uri, savePath, businessResponse,
          onReceiveProgress: onReceiveProgress);
    } on Exception catch (exception) {
      LogUtil.stdPrint(exception);
      CodeMsgModel codeMsgModel = CodeMsgModel();
      codeMsgModel.code = Constant.fail.toString();
      codeMsgModel.msg = exception.toString();
      businessResponse.onFail(codeMsgModel);
    }
  }
}
